/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dailydev.wave.example;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;

import com.google.wave.api.Annotation;
import com.google.wave.api.Element;
import com.google.wave.api.EventType;
import com.google.wave.api.impl.BlipData;
import com.google.wave.api.impl.ElementSerializer;
import com.google.wave.api.impl.EventData;
import com.google.wave.api.impl.EventDataSerializer;
import com.google.wave.api.impl.EventMessageBundle;
import com.google.wave.api.impl.EventMessageBundleSerializer;
import com.google.wave.api.impl.OperationSerializer;
import com.google.wave.api.impl.WaveletData;
import com.metaparadigm.jsonrpc.JSONSerializer;
import com.metaparadigm.jsonrpc.MarshallException;

/**
 * @author Roman Bosak, DailyDev.org
 * 
 */
public class ParrotyRobotSystemTest {

    private JSONSerializer serializer;

    /**
     * 
     */
    public ParrotyRobotSystemTest() {
        super();

        try {
            serializer = new JSONSerializer();
            serializer.registerDefaultSerializers();
            serializer.registerSerializer(new EventMessageBundleSerializer());
            serializer.registerSerializer(new EventDataSerializer());
            serializer.registerSerializer(new ElementSerializer());
            serializer.registerSerializer(new OperationSerializer());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testHandleWaveletSelfAdded() throws IOException, MarshallException {
        String robotUri = "http://localhost:8080/_wave/robot/jsonrpc";

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(robotUri);
        httpPost.addHeader("Content-Type", "application/json; charset=utf-8");

        String json = buildBundle("jytu");

        httpPost.setEntity(new ByteArrayEntity(json.getBytes()));

        HttpResponse response = httpClient.execute(httpPost);

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

    }

    private String buildBundle(String robotUri) throws MarshallException {
        EventMessageBundle eventBundle = new EventMessageBundle();

        BlipData rootBlip = new BlipData();
        rootBlip.setBlipId("b+plILR_KI%L");
        rootBlip.setLastModifiedTime(1249082216804l);
        ArrayList<String> contributors = new ArrayList<String>();
        contributors.add("bosy-test@wavesandbox.com");
        rootBlip.setContributors(contributors);
        rootBlip.setWaveletId("wavesandbox.com!conv+root");
        rootBlip.setWaveId("wavesandbox.com!w+plILR_KI%K");
        rootBlip.setVersion(27l);
        rootBlip.setCreator("bosy-test@wavesandbox.com");
        rootBlip.setContent("This is a root blip.");
        rootBlip.setAnnotations(new ArrayList<Annotation>());
        rootBlip.setElements(new HashMap<String, Element>());
        rootBlip.setChildBlipIds(new ArrayList<String>());

        eventBundle.addBlip(rootBlip.getBlipId(), rootBlip);

        EventData eventData = new EventData(EventType.WAVELET_SELF_ADDED, "bosy-test@wavesandbox.com", 1249082220132l);
        HashMap<String, Object> eventdataProps = new HashMap<String, Object>();
        eventdataProps.put("blipId", "b+plILR_KI%L");
        eventdataProps.put("participantsRemoved", new ArrayList<String>());
        eventdataProps.put("participantsAdded", new ArrayList<String>(Collections
                .singletonList("wave-assistant@appspot.com")));
        eventData.setProperties(eventdataProps);

        eventBundle.addEvent(eventData);

        eventData = new EventData(EventType.WAVELET_PARTICIPANTS_CHANGED, "bosy-test@wavesandbox.com", 1249082220132l);
        eventdataProps = new HashMap<String, Object>();
        eventdataProps.put("blipId", "b+plILR_KI%L");
        eventdataProps.put("participantsRemoved", new ArrayList<String>());
        eventdataProps.put("participantsAdded", new ArrayList<String>(Collections
                .singletonList("wave-assistant@appspot.com")));
        eventData.setProperties(eventdataProps);

        eventBundle.addEvent(eventData);

        WaveletData waveletData = new WaveletData();
        waveletData.setLastModifiedTime(1249082220132l);
        waveletData.setTitle("Test of the Wave");
        waveletData.setWaveletId("wavesandbox.com!conv+root");
        waveletData.setRootBlipId(rootBlip.getBlipId());
        waveletData.setDataDocuments(new HashMap<String, String>());
        waveletData.setCreationTime(1249082203432l);
        waveletData.setWaveId("wavesandbox.com!w+plILR_KI%K");
        waveletData.setParticipants(new ArrayList<String>(Arrays.asList("bosy-test@wavesandbox.com",
                "wave-assistant@appspot.com")));
        waveletData.setCreator("bosy-test@wavesandbox.com");
        waveletData.setVersion(30l);

        eventBundle.setWaveletData(waveletData);

        String json = serializer.toJSON(eventBundle);

        return json;
    }
}
